// import calculator from '../product/productdetail.js';

require(['../config'], function () {
  require([
    'jquery',
    'avalon',
    'util',
    'loan/product-apply',
    'page',
    'Cookies', 'text!/common/nav.html', 'text!/common/footer.html', 'navInit',
    'calculator'
  ], function ($, avalon, util, productApply, paging, Cookies,
    navHtml,
    footerHtml,
    navInit) {


      $(".global-nav").html(navHtml);
      $(".global-footer").html(footerHtml);
      navInit();

      var dict = {
        payment: function (index) {
          return ['等额本金', '等额本息', '先息后本', '一次性还本付息'][index]
        },
        takeTimes: function (index) {
          return [
            '一天以内',
            '三天以内',
            '五天以内',
            '一周以内',
            '半月以内',
            '一月以内',
            '三月以内'
          ][index]
        }
      }

      var page1 = paging()

      var productId = util.getUrlParam('id')
      if (!productId) {
        location.href = '/loan/product.html'
      }

      // 产品详情
      var productDetailVm = avalon.define({
        $id: 'product-detail',
        product: {
          // 产品信息
          productName: '',
          count: '',
          imgUrl: '',
          money: '',
          rate: '',
          rateMin: '',
          paymentText: '',
          featuresText: '',
          requirement: '',
          material: ''
        },
        ispulloff: false,
        loadData: function () {
          // 获取产品信息
          util
            .ajax({
              url: '/loan/product/' + productId
            })
            .then(function (rsp) {
              console.log(rsp)
              var featuresText = ''
              var textArray = rsp.data.featuresText.split(',')
              $.each(rsp.data.features.split(','), function (i, str) {
                featuresText +=
                  '<span class="f' + str + '">' + textArray[i] + '</span>'
              })

              if (!rsp.data.paymentText) {
                rsp.data.paymentText = ''
                var paymentResArr = []
                var paymentArr = rsp.data.payment.split(',')
                for (var i = 0; i < paymentArr.length; i++) {
                  paymentResArr.push(dict.payment(paymentArr[i]))
                }
                rsp.data.paymentText = paymentResArr.join(',')
              }

              productDetailVm.product = {
                productName: rsp.data.instName + '-' + rsp.data.name,
                count: rsp.data.count,
                imgUrl: rsp.data.logo || 'http://img0.rongsue.com/images/channel/rec-logo.png',
                money:
                  (rsp.data.loanMin || '-') +
                  '~' +
                  (rsp.data.loanMax || '-') +
                  '万',
                rate: rsp.data.rateMin + '~' + rsp.data.rateMax + '%',
                loanTime:
                  rsp.data.loanTimeMin + '~' + rsp.data.loanTimeMax + '月',
                rateMinText: rsp.data.rateMin + '%',
                paymentText: rsp.data.paymentText.replace(/,/g, '、'),
                takeTimesText: rsp.data.takeTimesText,
                serviceFeeText: rsp.data.serviceFee + '%',
                featuresText: featuresText,
                requirement: rsp.data.requirement.replace(/\n/g, '<br>'),
                material: rsp.data.material.replace(/\n/g, '<br>')
              }
              // 获取推荐产品
              productVm.params.loanType = rsp.data.loanType
              productVm.queryRecommendList()
            })
        }
      })

      // 渠道列表
      var managersVm = avalon.define({
        $id: 'product-managers',
        managers: [],
        showMore: false,
        managerId: '',
        productId: productId,
        star: function (score) {
          var list = []
          for (var i = score || 5; i--;) {
            list.push('<i class="iconfont icon-star"></i>')
          }
          return list.join('')
        },
        apply: function (managerId) {
          productApply.apply(managerId, productId)
        },
        loadData: function () {
          // 获取渠道推荐
          util
            .ajax({
              url: '/loan/product/' + productId + '/manager/list',
              data: {
                limit: 6
              }
            })
            .then(function (rsp) {
              var arr = rsp.data.rows
              if (arr.length === 0) {
                productDetailVm.ispulloff = true
              }
              if (arr.length > 4) {
                managersVm.showMore = true
              }
              arr = arr.splice(0, 4)
              managersVm.managers = arr
            })
        },
        submit: function () {
          productApply.submit()
        }
      })

      // 产品推荐
      var productVm = avalon.define({
        $id: 'product-recommend',
        data: {},
        pageItems: [1, 2, 3, 4, 5],
        params: {
          page: 0,
          limit: 8,
          orderBy: '99',
          loanType: ''
        },
        avatar: function () {
          return this.data.avatar || '/assets/images/product/channel.png'
        },
        imgUrl: function (item) {
          return item.logo || 'http://img0.rongsue.com/images/channel/rec-logo.png'
        },
        productName: function (item) {
          return item.instName + '-' + item.name
        },
        info: function (item) {
          return item.takeTimesText + '内放款，利率' + item.rateMin + '%'
        },
        queryRecommendList: function () {
          var data = productVm.$model
          util
            .ajax({
              url: '/loan/product/recommend/list',
              type: 'post',
              data: data.params
            })
            .then(function (rsp) {
              productVm.data = rsp.data
            })
        }
      })

      // 产品评价
      var commentVm = avalon.define({
        $id: 'comment-list',
        data: {},
        pageItems: [1, 2, 3, 4, 5],
        params: {
          page: 1,
          limit: 6
        },
        star: function (score) {
          var list = []
          for (var i = score || 5; i--;) {
            list.push('<i class="iconfont icon-star"></i>')
          }
          return list.join('')
        },
        diaplayTime: function (time) {
          return util.diaplayTime(time)
        },
        queryCommentList: function () {
          var data = commentVm.$model
          util
            .ajax({
              url: '/loan/comment/product/' + productId + '/list',
              data: data.params
            })
            .then(function (rsp) {
              commentVm.data = rsp.data
              page1.init(commentVm.params.page, rsp.data.total)
            })
        }
      })

      $(function () {
        productDetailVm.loadData()
        managersVm.loadData()
        commentVm.queryCommentList()

        page1.init(
          1,
          0,
          $('#page1'),
          function () {
            // 回调函数内可使用$(this).data('page')获取准备翻到的那一页
            commentVm.params.page = $(this).data('page')
            commentVm.queryCommentList()
          },
          commentVm.params.limit
        )

        $('#three').hide()
        //tab切换
        var tabs = $('.tabs li a')
        tabs.click(function () {
          var content = $(this).data('ref')
          tabs.removeClass('active')
          $(this).addClass('active')
          $('#content')
            .find('.content-wrap')
            .hide()
          $('#' + content).fadeIn(200)
        })
        // dialog show
        $('.aside_calc').on('click', function () {
          $('#dialog').show()
        })
        // dialog hide
        $('#close-btn').on('click', function () {
          $('#dialog').hide()
        })
      })

      avalon.scan(document.body)
    })
})
